package com.sl.au.service.impl;

import java.util.List;
import java.util.Map;

import javax.persistence.Query;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sl.au.entity.Chance;
import com.sl.au.entity.ExpectedOrder;
import com.sl.au.entity.ExpectedOrderToChance;
import com.sl.au.repository.ExpectedOrderRepository;
import com.sl.au.repository.ExpectedOrderToChanceRepository;
import com.sl.au.service.ExpectedOrderService;
import com.sl.au.service.ExpectedOrderToChanceService;
import com.sl.au.service.QueryService;

@Service("ExpectedOrderToChanceService")
@Transactional
public class ExpectedOrderToChanceServiceImpl extends BaseServiceImpl implements ExpectedOrderToChanceService {

	@Autowired
	ExpectedOrderToChanceRepository expectedOrderToChanceRepository;
	
	@Autowired
	QueryService queryService;

	public ExpectedOrderToChanceRepository getRepository() {
		return this.expectedOrderToChanceRepository;
	}

	@Override
	public List<ExpectedOrderToChance> getExpectedOrders(int start, int length, Map<String, String> condition, String orderName,
			String dir) {
		String key = condition.get("key");
		Page<ExpectedOrderToChance> pages = null;
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "id");
		pages = expectedOrderToChanceRepository.findExpectedOrder(pageable);
		return pages.getContent();
	}

	@Override
	public int getExpectedOrdersCount(Map<String, String> condition) {
		StringBuilder hql = new StringBuilder("from ExpectedOrder eo , Chance c where c.id = eo.chance.id");
		Query query = queryService.createQueryHql(hql.toString());
		return query.getResultList().size();
	}

	@Override
	public List<ExpectedOrderToChance> getExpectedOrder(int start, int length, Map<String, String> condition, String orderName,
			String dir) {
		Page<ExpectedOrderToChance> pages = null;
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "id");
		//权限控制
		if (condition.get("dataFilter").equals("所有"))
			pages = expectedOrderToChanceRepository.findAll(condition.get("key"), pageable);
		else if (condition.get("dataFilter").equals("本部门"))
			pages = expectedOrderToChanceRepository.findSuperiorPage(condition.get("key"), condition.get("superior"), pageable);
		else if (condition.get("dataFilter").equals("本片区"))
			pages = expectedOrderToChanceRepository.finAreaPage(condition.get("key"), condition.get("area"),pageable);
		else//个人
			pages = expectedOrderToChanceRepository.findSelfPage(condition.get("key"), condition.get("userId"), pageable);
		
		return pages.getContent();
	}

	@Override
	public long getExpectedOrderCount(Map<String, String> condition) {
		long count = 0;
		if (condition.get("dataFilter").equals("所有"))
			count = expectedOrderToChanceRepository.getAllCount(condition.get("key"));
		else if (condition.get("dataFilter").equals("本部门"))
			count = expectedOrderToChanceRepository.getSuperiorCount(condition.get("key"), condition.get("superior"));
		else if (condition.get("dataFilter").equals("本片区"))
			count = expectedOrderToChanceRepository.getAreaCount(condition.get("key"), condition.get("area"));
		else
			count = expectedOrderToChanceRepository.getSelfCount(condition.get("key"), condition.get("userId"));
		
		return count;
	}


}
